package com.dotflat.refund.controller;

import com.dotflat.fin.PO.PayRecordPO;
import com.dotflat.fin.domain.PayRecord;
import com.dotflat.fin.domain.RefundRecord;
import com.dotflat.refund.PO.RefundRecordPO;
import com.dotflat.refund.service.RefundRecordService;
import com.dotflat.rest.controller.RestfulBaseController;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.foundation.dto.teach.ChildDTO;
import com.matt.service.TeachCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.matt.foundation.constant.CommonConstant.TICKET;


/**
 * @author ：Ron
 * @date ：Created in 11/6/2020 5:07 PM
 */
@RestController
@RequestMapping("/fin/refund")
@Slf4j
@Api(tags = "财务 - 退费记录")
public class RefundRecordController extends RestfulBaseController<RefundRecord, RefundRecordService, RefundRecordPO> {

    @Autowired
    public void setBaseService(RefundRecordService service){
        this.baseService = service;
    }

    @Autowired
    private TeachCallService teachCallService;

    @ApiOperation(value = "获取一个对象")
    @GetMapping()
    public StatusResponse get(@RequestHeader(TICKET) String ticket, String id){

        StatusResponse res = super.get(ticket, id);

        RefundRecord record = (RefundRecord) res.getData();

        if (record == null){
            return res;
        }

        List<String> ids = new ArrayList<>();
        ids.add(record.getClienteleId());
        List<ChildDTO> children = teachCallService.getChildClassList(ticket, 1, null, null,ids);

        if (children.size() > 0){
            ChildDTO c = children.get(0);
            record.setChildName(c.getName());
            record.setClassName(c.getClassNames());
        }

        return res;
    }

    @ApiOperation(value = "获取列表")
    @GetMapping("/list")
    @Override
    public StatusResponse getList(@RequestHeader(TICKET) String ticket, RefundRecordPO param, PagerQuery pagerQuery){

        String classId = param.getClassId();
        String childrenName = param.getChildName();
        param.setChildName(null);
        param.setClassId(null); //需要置空，不然rest框架会自动查询这个不存在的字段，报错

        //获取教务系统班级id
        List<String> ids = null;
        List<ChildDTO> children = null;
        if (StringUtils.isNotBlank(classId) || StringUtils.isNotBlank(childrenName)){
            //有班级id的时候，限定客户id范围
            children = teachCallService.getChildClassList(ticket, 1, childrenName, classId,null);
            if (children != null && children.size() > 0) {
                ids = children.stream().map(o->o.getCrmClienteleId()).collect(Collectors.toList());
            }
            if (ids == null){
                //限定空范围，直接返回空
                Page result = new PageImpl(new ArrayList());
                return StatusResponse.success(result);
            }

            param.setClienteleIds(ids);
        }

        StatusResponse res = super.getList(ticket, param, pagerQuery);

        Page<RefundRecord> records = (Page<RefundRecord>) res.getData();

        if (records == null){
            return res;
        }

        //重新获取班级关系
        if (children == null || (children.size() == 0 && records.getSize() > 0)){
            //重新获取班级关系
            ids = records.getContent().stream().map(o-> o.getClienteleId()).collect(Collectors.toList());
            children = teachCallService.getChildClassList(ticket, 1, null, null, ids);
        }

        //重新装配班级信息
        if (children != null && children.size() > 0){
            List<ChildDTO> finalChildren = children;
            records.getContent().stream().forEach(o->{
                Optional<ChildDTO> op = finalChildren.stream().filter(c->o.getClienteleId().equals(c.getCrmClienteleId())).findFirst();
                if (op.isPresent()){
                    ChildDTO child = op.get();
                    o.setClassName(child.getClassNames());
                    o.setChildName(child.getName());
                }
            });
        }

        return res;
    }

}
